package com.douding.server.service;

import com.douding.server.domain.*;
import com.douding.server.dto.CourseDto;
import com.douding.server.dto.SectionDto;
import com.douding.server.dto.PageDto;
import com.douding.server.dto.SectionPageDto;
import com.douding.server.enums.SectionChargeEnum;
import com.douding.server.mapper.SectionMapper;
import com.douding.server.util.CopyUtil;
import com.douding.server.util.UuidUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

import java.util.List;


import java.util.Date;


@Service
public class SectionService {

    @Resource
    private SectionMapper sectionMapper;

    @Resource
    private CourseService courseService;


    /**
     * 列表查询
     */
    public void list(SectionPageDto sectionPageDto) {

    }


//    public void save(SectionDto sectionDto) {
//
//    }

    //新增数据
    private void insert(Section section) {


    }

    //更新数据
    private void update(Section section) {

    }

    public void delete(String id) {

    }

    /**
     * 查询某一课程下的所有节
     */
    public List<SectionDto> listByCourse(String courseId) {

        return null;
    }


    /**
     * 我完成的部分  开始
     */
    public void selectByPage(SectionPageDto pageSectionDto) {
        PageHelper.startPage(pageSectionDto.getPage(), pageSectionDto.getSize());
        List<Section> sectionList = sectionMapper.selectList();
        PageInfo<Section> pageInfo = new PageInfo<>(sectionList);
        pageSectionDto.setTotal(pageInfo.getTotal());
        List<SectionDto> sectionDtoList = CopyUtil.copyList(sectionList, SectionDto.class);
        pageSectionDto.setList(sectionDtoList);
    }

    public void save(SectionDto sectionDto) {
        Section section = new Section();
        BeanUtils.copyProperties(sectionDto, section);
        if (section.getId() != null) {
            // 更新
            section.setUpdatedAt(new Date());
            sectionMapper.update(section);
        } else {
            String id = UuidUtil.getShortUuid();
            section.setId(id);
            section.setCreatedAt(new Date());
            section.setUpdatedAt(new Date());
            // 新增
            sectionMapper.insert(section);
        }
    }

    public void deleteById(String id) {
        sectionMapper.deleteById(id);
    }

}
